package com.lottery.utils;

import com.lottery.dao.qiuqiu.accoutDao.CustomerServiceDao;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.lottery.entity.CustomerServiceInformation;



public class MyRealm extends AuthorizingRealm{
	
	@Autowired
	private CustomerServiceDao customerServiceDao;

	
	//认证
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken)arg0;
		String username = token.getUsername();
		char[] password = token.getPassword();
		CustomerServiceInformation customerServiceInformation = customerServiceDao.selectByName(username);
		if(customerServiceInformation==null){
			return null;
		}
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(customerServiceInformation,customerServiceInformation.getPassword(),this.getClass().getName());
		return info;
	}
	
	//授权
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		//授权信息对象
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		CustomerServiceInformation customer = (CustomerServiceInformation) arg0.getPrimaryPrincipal();
		if(customer.getServerType().equals("总管理员")){
			info.addStringPermission("admin");
		}else{
			info.addStringPermission("employee");
		}
		return info;
	}

	

}
